<?php

require_once 'Core/Command/Abstract.php';

class Core_Command_UpdateVersion19 extends Core_Command_Abstract
{
    /**
     * db adapter
     *
     * @var zend_db_table adapter
     */
    private $_db;

    /**
     * load the db adapter
     *
     */
    public function __construct()
    {
        $this->_db = Zend_Db_Table::getDefaultAdapter();
        parent::__construct();
    }

    /**
     *
     * create pages and content nodes tables
     * validate that there are pages in the content table.
     * migrate content table rows to pages / content_nodes
     *
     */
    public function run($params = null)
    {
        $result = $this->_updateTemplateReferences();
        if (!$result) {
            $this->log($this->view->getTranslation('ERROR: could not update content template references.'));
        } else {
            $this->log($this->view->getTranslation('Content template references updated OK.'));
        }
    }

    /**
     * returns details about the current command
     *
     */
    public function info()
    {
        $this->log($this->view->getTranslation('The Update Version 19 command will update your database from version 1.8 to 1.9'));
        $this->log($this->view->getTranslation('Params: none'));
    }

    private function _updateTemplateReferences()
    {
        $this->_db->query("ALTER TABLE `" . Core_Db_Table::getTableName('users') . "` ADD `openid` VARCHAR(100) NOT NULL AFTER `email`");
        $this->_db->query("UPDATE `"      . Core_Db_Table::getTableName('pages') . "` SET `publish_level` = '1' WHERE `publish_level` = 0");
        $siteSettings = new Model_SiteSettings();
        $siteSettings->set('xml_declaration', 'never');
        $siteSettings->set('publish_pages', 0);
        $siteSettings->save();

        return true;
    }
}